
//leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    /**
     * 第一遍
     * @param head
     * @return
     */
//    public ListNode deleteDuplicates(ListNode head) {
//        if (head == null) return null;
//        // 快慢指针
//        ListNode slow = head;
//        ListNode fast = head.next;
//        while (fast != null) {
//            if (slow.val != fast.val) {
//                slow.next = fast;
//                slow = slow.next;
//            }
//            fast = fast.next;
//        }
//        slow.next = null;
//        return head;
//    }










    /**
     * 第二遍 2/27
     * @param head
     * @return
     */
//    public ListNode deleteDuplicates(ListNode head) {
//        if (head == null) return null;
//
//        // 快慢指针
//        ListNode slow = head, fast = head.next;
//        while (fast != null) {
//            if (slow.val != fast.val) {
//                slow.next = fast;
//                slow = slow.next;
//            }
//            fast = fast.next;
//        }
//        slow.next = null;
//        return head;
//    }


    /**
     * 第三遍 3/1
     * @param head
     * @return
     */
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) return null;

        // 快慢指针
        ListNode slow = head, fast = head;
        while (fast != null) {
            if (slow.val != fast.val) {
                // 如果两者不相等，则slow指向fast
                slow.next = fast;
                // slow已经指向fast，让slow前进继续指向下一个
                slow = slow.next;
            }
            // 如果相等的话，则跳过,fast前进，slow不动
            fast = fast.next;
        }
        slow.next = null;
        // 返回原链表
        return head;
    }
}
//leetcode submit region end(Prohibit modification and deletion)
